package controllers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"new/kpg/app"
	"new/kpg/e"
	"new/models"
	"new/services"
)

//查找家族成员表信息
func Getfamily_member(c *gin.Context) {
	err, info, total := services.GetAllfamily_member()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total}, "查询成功")
}

//删除家族成员表信息
func Delfamily_member(c *gin.Context) {
	b, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(b, &m)
	id := com.StrTo(m["Id"]).MustInt()
	if id == -1 {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.Delfamily_member(id)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "删除成功")
}

//更改家族成员表信息
func Updatefamily_member(c *gin.Context) {
	b, _ := c.GetRawData() //获取原始字节
	var m map[string]string
	_ = json.Unmarshal(b, &m) //将结构类型转换为json字符串
	if m["Id"] == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}

	Id := m["id"]
	F_name := m["F_name"]
	Birth_data := m["Birth_data"]
	Life := m["Life"]
	Glory := m["Glory"]

	if Id == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updatefamily_member(models.Family_member{Id, F_name, Birth_data, Life, Glory})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "更新成功")
}

//增加家族成员表信息
func Addfamily_member(c *gin.Context) {
	b, _ := c.GetRawData() //获取原始字节
	var m map[string]string
	_ = json.Unmarshal(b, &m) //将结构类型转换为json字符串

	Id := m["Id"]
	F_name := m["F_name"]
	Birth_data := m["Birth_data"]
	Life := m["Life"]
	Glory := m["Glory"]
	if Id == "" {
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addfamily_member(models.Family_member{Id, F_name, Birth_data, Life, Glory})
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, map[string]interface{}{}, "添加成功")
}
